package com.twu.biblioteca;

import java.util.Date;
import java.util.Scanner;


public class BibliotecaApp {

    public static int numberOfCustomer = 2;
    public static boolean loginStatus = false;
    public static int logInInfo=-1;
    static  Customer[] customers;

    public static void menu(int Item_number, String Menuitem) {
        System.out.println("[" + Item_number + "]" + Menuitem);
    }
    public static void collectionOfMemberOfLibrary(Customer[] customers,String name, Date dateOfBirth, Date dateOfMembership, String address, long contact, String email,String password,int noOfCustomer) {
        String userName;
            userName= generateLibraryNo(noOfCustomer);
            customers[noOfCustomer]=new Customer(name, dateOfBirth, dateOfMembership, address, contact,email,userName,password);
    }

    public static Date getTodayDate() {
        Date today;
        java.util.Calendar cal = java.util.Calendar.getInstance();
        today = cal.getTime();
        return today;
    }

    public static String generateLibraryNo(int noOfCustomer) {
        String libraryNo = "111-1111";
        String libraryNoPart1, libraryNoPart2;
        Integer libraryNumberInInteger;
        if (noOfCustomer == 1) {
            return libraryNo;
        } else {
            libraryNoPart1 = libraryNo.substring(0, 3);
            libraryNoPart2 = libraryNo.substring(4, 8);
            libraryNo = libraryNoPart1 + libraryNoPart2;
            libraryNumberInInteger = Integer.parseInt(libraryNo);
            libraryNumberInInteger = libraryNumberInInteger + (noOfCustomer-1);
            libraryNo = new StringBuilder(libraryNumberInInteger.toString()).insert(libraryNumberInInteger.toString().length()-4, "-").toString();
            return libraryNo;
        }


    }

    public static void checkUsernamePassword(String userName,String password) {
        for (int i = 1; i <= numberOfCustomer; i++) {
            if(customers[i].getLibraryNo().equals(userName) && customers[i].getPassword().equals(password))
            {
                        loginStatus=true;
                        logInInfo=i;
            }

        }

    }

    public static void displayLoginScreen(Scanner scanner) {
        String userName="", password="";
        System.out.print("Enter Your UserName(LibraryNumber): ");
        try {
            userName = scanner.next();
        } catch (Exception e) {
            System.err.print("Error in reading value");
        }
        System.out.print("Enter Your password: ");
        try {
            password = scanner.next();
        } catch (Exception e) {
            System.err.print("Error in reading value");
        }
        checkUsernamePassword(userName,password);
        if (loginStatus==false)
            System.out.println("Check Your Login and Password");
    }

    public static void main(String[] args) {
        int menuOption = 0, menuOption1 = 0, menuOption2 = 0;
        char answer = 'y';
        Book[] books = new Book[6];
        Customer customer = new Customer();
        customers = new Customer[numberOfCustomer+1];
        collectionOfMemberOfLibrary(customers,"Apurva", getTodayDate(), getTodayDate(), "dholpur", 88l, "apurvagupt@gmail.com","apurvagupta",1);
        collectionOfMemberOfLibrary(customers,"Kavita", getTodayDate(), getTodayDate(), "Bundi", 68l, "kav@gmail.com","kavitagupta",2);
        numberOfCustomer=2;
        LibraryShelves libraryShelves = new LibraryShelves();
        Scanner scanner = new Scanner(System.in);
        while (answer == 'y' || answer == 'Y') {
            System.out.println("Welcome to our BIBLIOTECA Library!");
            System.out.println("******* ** *** MENU *** ** *******");
            menu(1, "List of books in our library");
            menu(2, "Membership details");
            menu(3, "Movie Details");
            if(loginStatus==false)
                menu(4, "Log In");
            else
                menu(4,"Log Out");
            System.out.println("\n Enter your menu option: ");
            try {
                menuOption = scanner.nextInt();
            } catch (Exception e) {
                System.err.print("Error in reading int value");
            }

            if (menuOption == 2 && loginStatus == false)
                customer.membershipDetails();
            else if (menuOption == 2 && loginStatus == true)
            {
                   customers[logInInfo].membershipDetails();

            }

            else if (menuOption == 1) {


                libraryShelves.showBooks();

                System.out.println("\n Enter your menu option: ");
                try {
                    menuOption1 = scanner.nextInt();
                } catch (Exception e) {
                    System.err.print("Error in reading int value");
                }

                if (menuOption1 < LibraryShelves.totalUniqueBooks && menuOption1 > 0) {
                    System.out.println("******* ** *** MENU *** ** *******");
                    menu(1, "Reserve");
                    menu(2, "Exit");
                    System.out.println("\n Enter your menu option: ");
                    try {
                        menuOption2 = scanner.nextInt();
                    } catch (Exception e) {
                        System.err.print("Error in reading int value");
                    }

                    if (menuOption2 == 1) {
                        LibraryShelves.collectionOfBooks(books);
                        customer.reserveBook(books[menuOption1]);
                    } else if (menuOption2 == 2)
                        System.exit(0);
                    else
                        System.out.println("select valid option!!");

                } else
                    System.out.println("select valid option!!");
            }
            else if (menuOption == 3)
                libraryShelves.showMovies();
            else if (menuOption == 4 && loginStatus ==false)
                displayLoginScreen(scanner);
            else if (menuOption == 4 && loginStatus ==true)
                    loginStatus =false;
            else
                System.out.println("select valid option!!");

            System.out.println("Do you want to continue<y/n>");
            try {
                answer = (char) System.in.read();
            } catch (Exception e) {
                System.err.print("Error in reading int value");
            }
        }

    }
}
